package com.foreknow.elm.dao.Impl;

import com.foreknow.elm.dao.BusinessDao;
import com.foreknow.elm.po.Business;
import com.foreknow.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BusinessDaoImpl implements BusinessDao {
    private Connection conn;
    private PreparedStatement pst;
    private ResultSet rs;
    @Override
    public List<Business> listBusinessByOrderTypeId(Integer orderTypeId) throws SQLException {
        List<Business> list=new ArrayList<>();
        String sql="select * from business where orderTypeId=?";
        try {
//            Connection从ThreadLocal中获取
            conn=DBUtil.getConnection();
            pst=conn.prepareStatement(sql);
//            将？替换为具体的值
            pst.setInt(1,orderTypeId);
//            执行查询并将查询到结果的放在结果集里
            rs=pst.executeQuery();
//            遍历结果集并将每行数据保存到List集合中
            while (rs.next()){
                Business bu=new Business();
                bu.setBusinessId(rs.getInt("BusinessId"));
                bu.setBusinessName(rs.getString("BusinessName"));
                bu.setBusinessAddress (rs.getString("BusinessAddress"));
                bu.setBusinessExplain(rs.getString("BusinessExplain"));
                bu.setBusinessImg(rs.getString("BusinessImg"));
                bu.setOrderTypeId(rs.getInt("OrderTypeId"));
                bu.setStarPrice(rs.getDouble("StarPrice"));
                bu.setDeliveryPrice(rs.getDouble("DeliveryPrice"));
                bu.setRemarks(rs.getString("Remarks"));
                list.add(bu);
            }
        }finally {
            DBUtil.close(pst);
        }
        return list;
    }

    @Override
    public Business getBusinessById(Integer businessId) throws SQLException {
        Business business=new Business();
        String sql="select * from business where businessId=?";
        try {
            conn=DBUtil.getConnection();
            pst=conn.prepareStatement(sql);
            pst.setInt(1,businessId);
            rs=pst.executeQuery();
            while (rs.next()){
                business.setBusinessId(rs.getInt("BusinessId"));
                business.setBusinessName(rs.getString("BusinessName"));
                business.setBusinessAddress (rs.getString("BusinessAddress"));
                business.setBusinessExplain(rs.getString("BusinessExplain"));
                business.setBusinessImg(rs.getString("BusinessImg"));
                business.setOrderTypeId(rs.getInt("OrderTypeId"));
                business.setStarPrice(rs.getDouble("StarPrice"));
                business.setDeliveryPrice(rs.getDouble("DeliveryPrice"));
                business.setRemarks(rs.getString("Remarks"));
            }
        }finally {
            DBUtil.close(pst);
        }
        return business;
    }

    public static void main(String[] args) throws SQLException {
        BusinessDaoImpl businessDao=new BusinessDaoImpl();
//        List<Business> list=businessDao.listBusinessByOrderTypeId(1);
//        for (int i=0;i<list.size();i++){
//            System.out.println(list.get(i).getBusinessName());
//        }
        Business business=businessDao.getBusinessById(10005);
        System.out.println(business.getBusinessName());
    }
}
